package com.sleepcamel.resteasyConfigurator.util;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GSONHierarchySerializer<T> implements JsonSerializer<T>, JsonDeserializer<T> {
	    @Override
	    public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
	        JsonObject result = new JsonObject();
	        result.add("type", new JsonPrimitive(src.getClass().getName()));
	        result.add("properties", context.serialize(src, src.getClass()));
	 
	        return result;
	    }
	 
	    @SuppressWarnings("unchecked")
		@Override
	    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
	        JsonObject jsonObject = json.getAsJsonObject();
	        String type = jsonObject.get("type").getAsString();
	        JsonElement element = jsonObject.get("properties");
	 
	        try {
	        	Class<?> forName = Class.forName(type);
	            return (T)context.deserialize(element, forName);
	        } catch (ClassNotFoundException cnfe) {
	            throw new JsonParseException("Unknown element type: " + type, cnfe);
	        }
	    }
}
